export default {
  state: {
    arr: [{
      title: "地三鲜",
      price: 8.82,
      pricez: 9,
      n: 0,
      src: require("../assets/图1.png"),
      xiaoliang: 62,
      haoping: "100%",
      id: 1
    }, {
      title: "酸菜粉",
      price: 8.82,
      pricez: 9,
      n: 0,
      src: require("../assets/图2.png"),
      xiaoliang: 74,
      haoping: "100%",
      id: 2
    }, {
      title: "肉段烧茄子",
      price: 18.42,
      pricez: 24,
      n: 0,
      src: require("../assets/图3.png"),
      xiaoliang: 15645,
      haoping: "98%",
      id: 3
    }, {
      title: "地三鲜",
      price: 8.82,
      pricez: 9,
      n: 0,
      src: require("../assets/图1.png"),
      xiaoliang: 62,
      haoping: "100%",
      id: 4
    }, {
      title: "酸菜粉",
      price: 8.82,
      pricez: 9,
      n: 0,
      src: require("../assets/图2.png"),
      xiaoliang: 74,
      haoping: "100%",
      id: 5
    }, {
      title: "肉段烧茄子",
      price: 18.42,
      pricez: 24,
      n: 0,
      src: require("../assets/图3.png"),
      xiaoliang: 15645,
      haoping: "98%",
      id: 6
    }],
    arrNew: [],
    xianshi: false,
    value:"",
    value1:""
  },
  mutations: {
    /* 加 */
    jia(state, index) {
      state.arr[index].n++
      if (state.arr[index].n > 0) {
        state.arrNew.forEach(item => {
          if (item.id == state.arr[index].id) {
            state.arrNew.splice(item, 1)
          }
        })
        state.arrNew.push(state.arr[index])
        // var bool = false
        // for(i=0;i<state.arr.length;i++){
        //   if(state.arrNew[i].id==state.arr[index].id){
        //       bool=true
        //   }
        // }
        // if(bool==false){
        //   state.arrNew.push(state.arr[index])
        // }
      }
    },
    /* 减 */
    jian(state, index) {
      state.arr[index].n--
      state.arrNew.forEach(item => {
        if (state.arr[index].n == 0) {
          if (item.id == state.arr[index].id) {
            state.arrNew.splice(item, 1)
          }
        }
      })
    },

  },
  getters: {
    /* 计算总价 */
    getsum(state) {
      var sum = 0
      state.arr.forEach(item => {
        sum += item.price * item.n
      });
      return sum
    }
  }
}
